package com.sau.pixelcart.controller;


import com.sau.pixelcart.common.context.UserContext;
import com.sau.pixelcart.common.result.PageResult;
import com.sau.pixelcart.common.result.Result;
import com.sau.pixelcart.domain.dto.InventoryPageDTO;
import com.sau.pixelcart.domain.dto.ProductDTO;
import com.sau.pixelcart.domain.dto.ProductPageDTO;
import com.sau.pixelcart.domain.entity.ProductInventory;
import com.sau.pixelcart.domain.vo.InventoryVO;
import com.sau.pixelcart.domain.vo.InventoryViewVO;
import com.sau.pixelcart.service.IProductInventoryService;
import com.sau.pixelcart.service.impl.ProductInventoryServiceImpl;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 商品实时库存表 前端控制器
 * </p>
 *
 * @author Inton
 * @since 2025-03-08
 */
@RestController
@RequestMapping("/inventory")
@Slf4j
public class ProductInventoryController {

    @Autowired
    private IProductInventoryService productInventoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 批量入库
     * @param productDTOList
     * @return
     */
    @PostMapping("/saveBatch")
    public Result saveBatch(@RequestBody List<ProductDTO> productDTOList){
        log.info("saveList 批量入库：{}",productDTOList);

        //清除redis缓存
        String key = "product_*";
        Set keys = redisTemplate.keys(key);
        if(keys != null && keys.size() > 0){
            log.info("清除redis缓存");
            redisTemplate.delete(keys);
        }

        productInventoryService.saveList(productDTOList);
        return Result.success();
    }

    /**
     * 库存界面分页查询
     * @param inventoryPageDTO
     * @return
     */
    @PostMapping("/list")
    public Result list(@RequestBody InventoryPageDTO inventoryPageDTO) {
        log.info("库存状态查询:{}",inventoryPageDTO);
        PageResult<InventoryVO> inventoryVOPageResult = productInventoryService.pageQuery(inventoryPageDTO);
        return Result.success(inventoryVOPageResult);
    }

    /**
     * 库存状态
     * @return
     */
    @GetMapping("/status")
    public Result inventoryStatus(){
        InventoryViewVO inventoryViewVO = productInventoryService.getView();
        return Result.success(inventoryViewVO);
    }

}
